#include <sys/socket.h>
#include <pcap.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "distriethhdr.h"
#include "packet_recv.h"

void packet_recv_loop(int s,struct sockaddr_in addr,void (*process)(u_char *args,const struct pcap_pkthdr *header,const u_char *packet))
{
	int len=128;

	char *buffer;
	int buffer_size=2000;

	buffer=(char*)malloc(buffer_size);	
	//buffer=(char*)malloc(MAX_PACKET);
	int addr_len =sizeof(struct sockaddr_in);
		
	bzero(buffer,buffer_size);
        //len =read(STDIN_FILENO,buffer,sizeof(buffer));
        sendto(s,"nidaye",7,0,(struct sockaddr *)&addr,addr_len); 


	while(1){

		len = recvfrom(s,buffer,buffer_size,0,(struct sockaddr *)&addr,&addr_len);
		
		struct pcap_pkthdr *header;
		header=(struct pcap_pkthdr *)malloc(sizeof(struct pcap_pkthdr));

		struct distriethhdr *dishdr;

		char *packet;

		dishdr=(struct distriethhdr*)buffer;
		header=(struct pcap_pkthdr *)(buffer+sizeof(struct distriethhdr));

	        packet=&buffer[sizeof(struct pcap_pkthdr) + sizeof(struct distriethhdr)];
		
		//packet[packet->len]='\0';

		(*process)(NULL,header,packet);
/*
		printf("receive:%d\n",sizeof(buffer));
	
		int i=0;

		for(i=0;i<16;i++)
		{
			printf("0x%x",buffer[i]);
		}
		printf("             ");
		for(i=16;i<32;i++)
		{
			printf("0x%x",buffer[i]);
		}
		printf("              ");
		for(i=32;i<40;i++)
		{
			printf("0x%x",buffer[i]);
		}

		printf("\n");			
*/	}

}
